package org.tooyoung.fourone.chat;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import org.tooyoung.fourone.R;

import java.util.List;

public class AddOrContactActivity extends AppCompatActivity {

    private static final String TAG = "AddOrContactActivity";
    private List<String> contactsFromServer;
    private String user;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 997:
                    Intent intent = new Intent(AddOrContactActivity.this, ChatActivity.class);
                    intent.putExtra("user", user);
                    startActivity(intent);
                    finish();
                    break;
                case 998:
                    linearLayout.setVisibility(View.VISIBLE);
                    txtName.setText(user);
                    break;
                case 999:
                    break;
            }
            return false;
        }
    });
    private ImageView imgLoading;
    private TextView txtName;
    private View view;
    private View linearLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_or_contact);

        user = getIntent().getStringExtra("user");
        Log.d(TAG, "user: " + user);
        txtName = (TextView) findViewById(R.id.user_name);
        view = findViewById(R.id.loading_layout);
        linearLayout = findViewById(R.id.linear_layout);
        imgLoading = (ImageView) findViewById(R.id.image_loading);
        AnimationDrawable loadingAnim = (AnimationDrawable) imgLoading.getBackground();
        loadingAnim.start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    contactsFromServer = EMClient.getInstance().contactManager().getAllContactsFromServer();
                    Log.d(TAG, "contactsFromServer: " + contactsFromServer.toString());
                    if (contactsFromServer.contains(user)) {
                        Message message = handler.obtainMessage(997);
                        Log.d(TAG, "run: 997");
                        handler.sendMessage(message);
                    } else {
                        Message message = handler.obtainMessage(998);
                        Log.d(TAG, "run: 998");
                        handler.sendMessage(message);
                    }
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void btnAdd(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().contactManager().addContact(user, "请求添加为好友");
                    Toast.makeText(AddOrContactActivity.this, "已发送添加请求，请等待", Toast.LENGTH_SHORT).show();
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}